<?php


namespace App\Zesow\Cache\Impl;

use App\Services\Business\Services\BusinessService;
use Illuminate\Support\Facades\Cache;
use App\Services\Business\Interfaces\CourseServiceInterface;

class CourseChapterCache
{

    public const KEY_NAME = 'course_chapter';


    /**
     * @var CourseService
     */
    protected $courseService;

    public function __construct(CourseServiceInterface $courseService)
    {
        $this->courseService = $courseService;
    }

    public function chapters(int $courseId)
    {

        if (Cache::has(self::KEY_NAME)) {
            return Cache::get(self::KEY_NAME);
        }
        $chapters = $this->courseService->chapters($courseId);
        Cache::put(self::KEY_NAME, $chapters);
        return $chapters;


    }

    public function destroy(int $courseId): void
    {
        Cache::forget($this->key($courseId));
    }

    private function key(int $courseId): string
    {
        return self::KEY_NAME . '_' . $courseId;
    }

}
